package cn.edu.hpu.jie.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import cn.edu.hpu.jie.entity.Candidate;
import cn.edu.hpu.jie.entity.CandidateSites;
import cn.edu.hpu.jie.services.CandidateService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CandidateAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private Map<String, Object> session = ActionContext.getContext().getSession();
	private CandidateService cs = new CandidateService();
	
	private Candidate candidate;
	private CandidateSites cands;
	private String name;
	private String studentid;
	private String sex;
	private String nation;
	private String birthday;
	private String college;
	private String major;
	private String politics;
	private String sites;
	private String title;
	private String subtitle;
	private String content;
	private List<Candidate> candidatelist;
	private String id;
	
	public String addCandidate() throws IOException {
		if("error".equals(getCandidateByStudentId())) {
			Candidate c = new Candidate();
			c.setBirthday(birthday);
			c.setCollege(college);
			c.setMajor(major);
			c.setName(name);
			c.setNation(nation);
			c.setPolitics(politics);
			c.setSex(Integer.parseInt(sex));
			c.setSites(sites);
			c.setStudentid(studentid);
			session.put("candidate", c);
			sendMsg("1");
			return null;
		}
		
		sendMsg("-1");
		return null;
	}
	
	public String goAddCandidateSites() {
		Candidate c = (Candidate) session.get("candidate");
		if(c != null) {
			studentid = c.getStudentid();
			return SUCCESS;
		}
		return ERROR;
	}
	
	
	public String addCandidateSites() throws IOException {
		if("POST".equals(ServletActionContext.getRequest().getMethod())) {
			if("error".equals(getCandidateByStudentId())) {
				Candidate c = (Candidate) session.get("candidate");
				if(c != null) {
					CandidateSites cands = new CandidateSites();
					cands.setContent(content);
					cands.setSubtitle(subtitle);
					cands.setTitle(title);
					
					sendMsg(cs.insertCandidate(c, cands, (Integer)session.get("loginid")) + "");
					session.remove("candidate");
				} else {
					sendMsg("2");
				}
				return null;
			}
			sendMsg("-1");
			return null;
		}
		return ERROR;
	}
	
	public String getCandidateByStudentId() {
		candidate = cs.getCandidateByStudentId(studentid);
		if(candidate == null) {
			return ERROR;
		}
		return SUCCESS;
	}
	
	private void sendMsg(String msg) throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println(msg);
		out.flush();
		out.close();
	}
	
	public String getCandidateList() {
		candidatelist = cs.getCandidateList();
		return SUCCESS;
	}
	
	public String goEditPage() {
		if(id != null) {
			candidate = cs.getCandidateById(Integer.parseInt(id));
			cands = cs.getCandidateSitesById(Integer.parseInt(id));
			return SUCCESS;
		}
		return NONE;
	}
	
	public String updateCandidate() throws IOException {
		if("POST".equals(ServletActionContext.getRequest().getMethod())) {
			Candidate c = new Candidate();
			c.setBirthday(birthday);
			c.setCollege(college);
			c.setMajor(major);
			c.setName(name);
			c.setNation(nation);
			c.setPolitics(politics);
			c.setSex(Integer.parseInt(sex));
			c.setSites(sites);
			c.setStudentid(studentid);
			c.setId(Integer.parseInt(id));
			
			sendMsg(cs.updateCandidateById(c, (Integer)session.get("loginid")) + "");
			return null;
		}
		
		return NONE;
	}
	
	public String updateCandidateSites() throws IOException {
		if("POST".equals(ServletActionContext.getRequest().getMethod())) {
			CandidateSites cands = new CandidateSites();
			cands.setContent(content);
			cands.setSubtitle(subtitle);
			cands.setTitle(title);
			cands.setId(Integer.parseInt(id));
			sendMsg(cs.updateCandidateSitesById(cands, (Integer)session.get("loginid")) + "");
			return null;
		}
		
		return NONE;
	}
	
	public String modifyEnable() throws IOException {
		if(id != null) {
			sendMsg(cs.modifyEnableById(Integer.parseInt(id), (Integer)session.get("loginid")) + "");
		}
		return null;
	}
	
	public String getCandidateSitesById() {
		System.out.println("hehe...");
		if(id != null) {
			cands = cs.getCandidateSitesById(Integer.parseInt(id));
			return SUCCESS;
		}
		return NONE;
	}
	
	public void validateGetCandidateSitesById() {
		System.out.println("isOk");
		if(id == null) {
			addActionError("id必填");
		} 
	}
	
	//set & get
	public void setSites(String sites) {
		this.sites = sites;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public void setStudentid(String studentid) {
		this.studentid = studentid;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public void setNation(String nation) {
		this.nation = nation;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public void setCollege(String college) {
		this.college = college;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public void setPolitics(String politics) {
		this.politics = politics;
	}

	public String getStudentid() {
		return studentid;
	}

	public Candidate getCandidate() {
		return candidate;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setSubtitle(String subtitle) {
		this.subtitle = subtitle;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public List<Candidate> getCandidatelist() {
		return candidatelist;
	}

	public void setId(String id) {
		this.id = id;
	}

	public CandidateSites getCands() {
		return cands;
	}
	
	
}
